//
//  LivestockSettingViewController.m
//  Trackit
//
//  Created by yaodaobin on 2017/4/10.
//  Copyright © 2017年 yaodaobin. All rights reserved.
//

#import "LivestockSettingViewController.h"

@interface LivestockSettingViewController ()<UITextFieldDelegate>

@property (nonatomic, strong) UITextField *intervalTextField;
@property (nonatomic, strong) UITextField *distanceTextField;

@end

@implementation LivestockSettingViewController

#pragma mark - Controller
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initParams];
    [self initUI];
}

#pragma mark - Init
- (void)initParams {
    
}

- (void)initUI {
    UIBarButtonItem *rightBtn = [UIBarButtonItem barItemWithTitle:NSLocalizedString(@"确定", nil) target:self action:@selector(submit)];
    self.navigationItem.rightBarButtonItem = rightBtn;

    kWeakSelf
    UILabel *intervalLabel = [[UILabel alloc] init];
    intervalLabel.font = [UIFont systemFontOfSize:14];
    intervalLabel.textColor = [UIColor lightGrayColor];
    intervalLabel.text = NSLocalizedString(@"航线采集间隔(单位:米)", nil);
    [self.view addSubview:intervalLabel];
    
    self.intervalTextField = [[UITextField alloc] init];
    self.intervalTextField.text = APP_DELEGATE.user.intervalValue;
    self.intervalTextField.delegate = self;
    self.intervalTextField.keyboardType = UIKeyboardTypeDecimalPad;
    self.intervalTextField.textColor = [UIColor grayColor];
    [self.view addSubview:self.intervalTextField];
    
    UIView *intervalLine = [[UILabel alloc] init];
    intervalLine.backgroundColor = [UIColor greenColor];
    [self.view addSubview:intervalLine];

    UILabel *distanceLabel = [[UILabel alloc] init];
    distanceLabel.font = intervalLabel.font;
    distanceLabel.textColor = intervalLabel.textColor;
    distanceLabel.text = NSLocalizedString(@"航线采集容错距离(单位:米)", nil);
    [self.view addSubview:distanceLabel];

    self.distanceTextField = [[UITextField alloc] init];
    self.distanceTextField.text = APP_DELEGATE.user.distanceValue;
    self.distanceTextField.delegate = self;
    self.distanceTextField.keyboardType = UIKeyboardTypeDecimalPad;
    self.distanceTextField.textColor = [UIColor grayColor];
    [self.view addSubview:self.distanceTextField];
    
    UIView *distanceLine = [[UILabel alloc] init];
    distanceLine.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:distanceLine];
    
    [intervalLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(40);
        make.leading.offset(20);
        make.trailing.offset(-20);
    }];
    
    [self.intervalTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(intervalLabel.mas_bottom).offset(5);
        make.leading.equalTo(intervalLabel.mas_leading);
        make.trailing.equalTo(intervalLabel.mas_trailing);
        make.height.mas_equalTo(40);
    }];
    
    [intervalLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.intervalTextField.mas_bottom);
        make.leading.equalTo(intervalLabel.mas_leading);
        make.trailing.equalTo(intervalLabel.mas_trailing);
        make.height.mas_equalTo(1);
    }];
    
    [distanceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(intervalLine.mas_bottom).offset(20);
        make.leading.equalTo(intervalLabel.mas_leading);
        make.trailing.equalTo(intervalLabel.mas_trailing);
    }];
    
    [self.distanceTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(distanceLabel.mas_bottom).offset(5);
        make.leading.equalTo(intervalLabel.mas_leading);
        make.trailing.equalTo(intervalLabel.mas_trailing);
        make.height.equalTo(weakSelf.intervalTextField.mas_height);
    }];
    
    [distanceLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.distanceTextField.mas_bottom);
        make.leading.equalTo(intervalLabel.mas_leading);
        make.trailing.equalTo(intervalLabel.mas_trailing);
        make.height.equalTo(intervalLine.mas_height);
    }];
}

#pragma mark - Action
- (void)submit {
    if (self.intervalTextField.text.length > 0) {
        APP_DELEGATE.user.intervalValue = self.intervalTextField.text;
        [LocationManager shareLocation].locationManager.desiredAccuracy = [self.intervalTextField.text floatValue];
    }
    if (self.distanceTextField.text.length > 0) {
        APP_DELEGATE.user.distanceValue = self.distanceTextField.text;
        [LocationManager shareLocation].locationManager.distanceFilter = [self.distanceTextField.text floatValue];

    }
    [self back];
}

#pragma mark - UITextField
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if (textField == self.intervalTextField) {
        [self.intervalTextField resignFirstResponder];
        [self.distanceTextField becomeFirstResponder];
    }
    return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (![string isEqual: @""] && [textField.text containsString:@"."]) {
        if ([string isEqual:@"."]) {
            return false;
        }
        if ([textField.text lengthOfBytesUsingEncoding:NSUTF8StringEncoding]) {
            if ([textField.text rangeOfString:@"."].location >= 3) {
                return false;
            }
        }
    }
    return true;
}


@end
